﻿using CallCenter.Data.Base;
using CallCenter.Data.BizUtilities.Base;
using CallCenter.Data.Models;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace CallCenter.Data.BizUtilities
{
    public class MenuUtility : BaseUtility
    {
        public MenuUtility(DataManager manager)
            : base(manager)
        { }

        public ReturnMessage<FeaturesVM> InsertToMenu(FeaturesVM model)
        {
            var returnMessage = new ReturnMessage<FeaturesVM>();
            //  if(this.ValidateScratchPadModel(model))
            //   {

            var id = this.Insert(model.FeatureText, model.FeatureDescription, model.NavigateUrl, model.ParentMenuId);

            List<RoleModelVM> roleModelVmList = this.Bridge.Role.GetRoles();
            //Write the code to insert into 

            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Message = "Record has been inserted";
                returnMessage.Status = true;

            }
            return returnMessage;
        }

        public ReturnMessage<FeaturesVM> UpdateToMenu(FeaturesVM model)
        {
            var returnMessage = new ReturnMessage<FeaturesVM>();
            //  if(this.ValidateScratchPadModel(model))
            //   {
            var id = this.Update(model.FeatureDescription,model.FeatureText,  model.NavigateUrl, model.ParentMenuId, model.id);
            if (id == -1)
            {
                returnMessage.Message = "We cannot insert data to database";
                returnMessage.Status = false;
            }
            else
            {
                model.id = id;
                returnMessage.Status = true;
            }
            return returnMessage;
        }

        public int Insert(string FeatureText, string description, string url, int parentmenuId) //insert data to a contact table
        {
            description = FeatureText;
            int nRtn = -1;
            string sql = "";
            if (parentmenuId != 0)
            {
                sql = "INSERT INTO tbl_Feature (FeatureText,Featuredescription, NavigateUrl, parentmenuId,Active) Values(@FeatureText,@description, @url, @parentmenuId,@Active) SELECT @pResult =@@IDENTITY";
            }
            else
            {
                sql = "INSERT INTO tbl_Feature (FeatureText,Featuredescription, NavigateUrl,Active) Values(@FeatureText,@description, @url,@Active) SELECT @pResult =@@IDENTITY";
            }

            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@description", SqlDbType.NVarChar, 500, description);
                this.Factory.AddParameter(cmd, "@url", SqlDbType.NVarChar, 100, url);
                this.Factory.AddParameter(cmd, "@FeatureText", SqlDbType.NVarChar, 500, url);
                this.Factory.AddParameter(cmd, "@Active", System.Data.SqlDbType.Bit, 1, 1);


                if (parentmenuId != 0)
                {
                    this.Factory.AddParameter(cmd, "@parentmenuId", SqlDbType.NVarChar, 100, parentmenuId);
                }

                var param = this.Factory.AddParameter(cmd, "@pResult", System.Data.SqlDbType.NVarChar, 500, null);
                param.Direction = System.Data.ParameterDirection.Output;

                this.Factory.ExecuteNonQuery(cmd);
                nRtn = Convert.ToInt32(param.Value);
            }
            return nRtn;

        }
        public int Update(string description, string FeatureText, string url, int parentmenuId, int id) //insert data to a title table
        {
            //int nRtn = -1;
            string sql = "UPDATE tbl_Feature SET FeatureText='" + FeatureText + "', FeatureDescription = '" + description + "', NavigateUrl = '" + url + "',ParentMenuId = '" + parentmenuId + "' WHERE id ='" + id + "'";

            //using (var cmd = this.Factory.GetCommand(sql))
            //{
            //    this.Factory.AddParameter(cmd, "@description", SqlDbType.NVarChar, 10, description);
            //    this.Factory.AddParameter(cmd, "@url", SqlDbType.NVarChar, 100, url);
            //    this.Factory.AddParameter(cmd, "@parentmenuId", SqlDbType.NVarChar, 100, parentmenuId);
            //    this.Factory.AddParameter(cmd, "@id", SqlDbType.Int, 10, id);
            this.Factory.ExecuteNonQuery(sql);

            //}
            //nRtn = id;
            return id; //retruning the data stored in the table
        }


        public void Delete(int titleId) //delete the row of data using the id passed to the function
        {
            string sql = "DELETE FROM tbl_Feature " +
                  "WHERE ID = @ID ";


            using (var cmd = this.Factory.GetCommand(sql))
            {
                this.Factory.AddParameter(cmd, "@ID", SqlDbType.Int, 10, titleId);
                this.Factory.ExecuteNonQuery(cmd);
            }
        }




        public List<FeaturesVM> GetAll() //Gets all the data form the contacts table
        {
            //string sql = "select tf.*,tf.Id as id,(select top(1) f.featureText from tbl_feature as f where f.id = tf.parentmenuid) as parentmenu from tbl_Feature as tf";
            string sql = "select tf.Id as id,isnull(tf.featureDescription,'') as FeatureDescription,isnull(tf.FeatureText,'') as FeatureText,isnull(tf.NavigateUrl,'') as NavigateUrl,isnull(tf.Active,'') as Active,isnull(tf.ParentMenuId,'') as ParentMenuId,isnull(tf.IsParentMenu,'') as IsParentMenu,(select top(1) f.featureText from tbl_feature as f where f.id = tf.parentmenuid) as parentmenu from tbl_Feature as tf";
            return this.Factory.ExecuteObjects<FeaturesVM>(sql);
        }

        public List<MainFaetureVm> GetMainMenuItems() //Gets all the data form the contacts table
        {
            string sql = "select f.Id,f.FeatureText as parentmenu from tbl_Feature f where parentmenuid is null";

            return this.Factory.ExecuteObjects<MainFaetureVm>(sql);
        }

        public List<FeaturesVM> GetMenuAccessSetUp()
        {
            string sql = "select * from tbl_feature";

            return this.Factory.ExecuteObjects<FeaturesVM>(sql);
        }


        public List<MenuVM> GetAllMenuByRoleID(int RoleId)
        {
            string sql = "select F.ID as MenuID,F.FeatureText as Menu,F.ParentMenuId as ParentID,R.Id as RoleID,RF.Active from tbl_feature as F" +
                            " join tbl_RoleFeature as RF on F.Id = RF.FeatureId" +
                            " join tbl_RolesData as R on RF.RoleId = R.ID" +
                            " where F.Active != 'False' and R.Id = '" + RoleId + "'order by parentid";
            return this.Factory.ExecuteObjects<MenuVM>(sql);
        }

        public string GetAllMenuByRoleID(int RoleId, int MenuID, string Status)
        {
            var result = "";
            string sql = "select  top(1) Active from tbl_RoleFeature where RoleId = '" + RoleId + "' and FeatureID = '" + MenuID + "'";
            result = this.Factory.ExecuteScalar(sql).ToString();

            if (result != "")
            {
                if (result == "False")
                    Status = "True";
                else
                    Status = "False";
                string _sql = "update tbl_RoleFeature set active = '" + Status + "' where RoleId = '" + RoleId + "' and FeatureID = '" + MenuID + "'";
                this.Factory.ExecuteNonQuery(_sql);
            }
            return Status;
        }
    }
}
